package edu.metrostate.capstonediner;

import android.media.SoundPool;
import android.media.SoundPool.*;
import android.media.AudioManager;
import android.content.res.AssetManager;
import java.io.IOException;

public class GameAudio {
	private AssetManager assetmanager;
	private SoundPool[] soundSets;
	private int[][] soundIds;
	private boolean muted;
	private boolean defaultMusicStarted;
	private boolean altMusicStarted;
	private int soundSet;
	
	public GameAudio(Controller controller, boolean muted, int soundSet){
		this.muted = muted;
		assetmanager = controller.getAssetManager();
		this.soundSet = soundSet;
		//dynamic loading will happen in the future, for now all assets are static
		soundIds = new int[2][2];
		soundSets = new SoundPool[2];
		soundSets[Constants.DEFAULT_SOUNDSET] = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
		soundSets[Constants.ALT_SOUNDSET] = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
		try {
			soundIds[Constants.DEFAULT_SOUNDSET][Constants.BACKGROUND_MUSIC] = soundSets[Constants.DEFAULT_SOUNDSET].load(assetmanager.openFd("music.ogg"), 1);
			soundIds[Constants.DEFAULT_SOUNDSET][Constants.CATCH_SOUND] = soundSets[Constants.DEFAULT_SOUNDSET].load(assetmanager.openFd("catch.ogg"), 1);		
			soundIds[Constants.ALT_SOUNDSET][Constants.BACKGROUND_MUSIC] = soundSets[Constants.ALT_SOUNDSET].load(assetmanager.openFd("music2.ogg"), 1);
			soundIds[Constants.ALT_SOUNDSET][Constants.CATCH_SOUND] = soundSets[Constants.ALT_SOUNDSET].load(assetmanager.openFd("catch2.ogg"), 1);				
		} catch (IOException e) {
			e.printStackTrace();
		}
		soundSets[Constants.DEFAULT_SOUNDSET].setOnLoadCompleteListener(new OnLoadCompleteListener() {
			public void onLoadComplete(SoundPool soundpool, int sampleid, int status) {
				if((sampleid == soundIds[Constants.DEFAULT_SOUNDSET][Constants.BACKGROUND_MUSIC] && getSoundSet() == Constants.DEFAULT_SOUNDSET)
						|| (sampleid == soundIds[Constants.ALT_SOUNDSET][Constants.BACKGROUND_MUSIC] && getSoundSet() == Constants.ALT_SOUNDSET))
					startMusic();
			}
		});
		soundSets[Constants.ALT_SOUNDSET].setOnLoadCompleteListener(new OnLoadCompleteListener() {
			public void onLoadComplete(SoundPool soundpool, int sampleid, int status) {
				if((sampleid == soundIds[Constants.DEFAULT_SOUNDSET][Constants.BACKGROUND_MUSIC] && getSoundSet() == Constants.DEFAULT_SOUNDSET)
						|| (sampleid == soundIds[Constants.ALT_SOUNDSET][Constants.BACKGROUND_MUSIC] && getSoundSet() == Constants.ALT_SOUNDSET))
					startMusic();
			}
		});
	}
	
	public void play(int soundId) {
		if(!muted)soundSets[soundSet].play(soundIds[soundSet][soundId], 1f, 1f, 1, 0, 1f);
	}
	
	public void startMusic() {
		if(!muted) {
			if(soundSet == Constants.DEFAULT_SOUNDSET) {
				soundSets[soundSet].play(soundIds[soundSet][Constants.BACKGROUND_MUSIC], 1f, 1f, 1, -1, 1f);
				defaultMusicStarted = true;
			}
			else {
				soundSets[soundSet].play(soundIds[soundSet][Constants.BACKGROUND_MUSIC], 1f, 1f, 1, -1, 1f);
				altMusicStarted = true;
			}

		}
	}
	
	public void pauseAll() {
		soundSets[soundSet].autoPause();
	}
	
	public void unpauseAll() {
		if(!muted)soundSets[soundSet].autoResume();
		if((soundSet == Constants.DEFAULT_SOUNDSET && !defaultMusicStarted) || (soundSet == Constants.ALT_SOUNDSET && !altMusicStarted) ) startMusic();
	}
	
	public void mute() {
		muted = true;
	}
	
	public void unMute() {
		muted = false;
	}
	
	public int getSoundSet() {
		return soundSet;
	}
	
	public void setSoundSet(int ss) {
		soundSet = ss;
		/*if(!muted) {
			if(soundSet == Constants.DEFAULT_SOUNDSET){
				soundpool.stop(soundIds[Constants.ALT_BACKGROUND_MUSIC]);
				soundpool.play(soundIds[Constants.BACKGROUND_MUSIC], 1f, 1f, 1, -1, 1f);
			}
			else{
				soundpool.stop(soundIds[Constants.BACKGROUND_MUSIC]);
				soundpool.play(soundIds[Constants.ALT_BACKGROUND_MUSIC], 1f, 1f, 1, -1, 1f);		
			}
		}*/
	}
	
}
